//package com.main.ok.hosp;
//
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.net.SocketTimeoutException;
//import java.security.cert.CertificateException;
//import java.security.cert.X509Certificate;
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//
//import javax.net.ssl.SSLContext;
//import javax.net.ssl.TrustManager;
//import javax.net.ssl.X509TrustManager;
//import javax.ws.rs.core.MediaType;
//
//import org.apache.commons.lang.CharEncoding;
//import org.apache.commons.lang.StringUtils;
//import org.apache.http.HttpEntity;
//import org.apache.http.HttpResponse;
//import org.apache.http.NameValuePair;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.client.utils.URLEncodedUtils;
//import org.apache.http.conn.scheme.Scheme;
//import org.apache.http.conn.ssl.SSLSocketFactory;
//import org.apache.http.entity.ContentType;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.impl.client.DefaultHttpClient;
//import org.apache.http.params.CoreConnectionPNames;
//import org.apache.http.util.EntityUtils;
//import org.apache.log4j.Logger;
//
//import com.main.ok.util.XmlConverter;
//
//public class ClientBase {
//    private static final Logger log = Logger.getLogger(ClientBase.class);
//
//    private static final String HEAD_NAME_SIGNATURE = "sign";
//    private static final String HEAD_NAME_TERMINAL_ID = "terminalId";
//    private static final String HEAD_NAME_OPERATOR = "operator";
//    private static final String HEAD_NAME_REQ_SERIAL_NO = "reqSerialNo";
//
//    public static final <T> String post(String url, T params, Class<T> classiz) throws Exception {
//        String response = null;
//        if (StringUtils.isBlank(url)) {
//            log.info("urlStr为空");
//        }
//        if (null == params) {
//            log.info("params为空");
//        }
//        int connectTimeout = 5000;
//        int soTimeout = 60000;
//        HttpPost post = new HttpPost("http://localhost:8080/epay/rest/" + url);
//        // 返回xml
//
//        // 推送内容，XML格式
//        post.addHeader("Accept", MediaType.TEXT_XML);
//        post.addHeader("Content-Type", "text/xml;charset=UTF-8");
//        String xml = XmlConverter.toXML(params, classiz);
//
//        log.info("request-xml:" + xml);
//        HttpEntity entity = new StringEntity(xml, ContentType.create(MediaType.TEXT_XML, CharEncoding.UTF_8));
//        // 发送请求
//        HttpClient httpclient = new DefaultHttpClient();
//        setHttps(httpclient);
//        // 链接超时
//        httpclient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
//        HttpResponse result = null;
//        try {
//            post.setEntity(entity);
//            result = execute(httpclient, post, soTimeout, 2);
//            log.info("responsecode:" + result.getStatusLine().getStatusCode());
//            response = EntityUtils.toString(result.getEntity(), CharEncoding.UTF_8);
//        } catch (Exception e) {
//            log.error("http调用失败,url:" + post.getURI() + "========request:" + xml);
//            throw new Exception(e);
//        } finally {
//            EntityUtils.consume(entity);
//            post.releaseConnection();
//            httpclient.getConnectionManager().shutdown();
//        }
//        if (null == result || null == result.getEntity()) {
//
//            throw new Exception("远程调用未返回:null == result || null == result.getEntity()");
//        }
//
//        log.info("response=============xml:" + response);
//        if (StringUtils.isBlank(response)) {
//            throw new Exception("response为null");
//        }
//        return response;
//    }
//
//    private static HttpResponse execute(HttpClient httpclient, HttpPost post, int soTimeout, int resendNum)
//            throws Exception {
//        try {
//            // 读取超时
//            httpclient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, soTimeout);
//            return httpclient.execute(post);
//        } catch (Exception e) {
//            int rsn = resendNum;
//            if (rsn > 0) {
//                if (e instanceof SocketTimeoutException) {
//                    soTimeout += 1000;
//                }
//                return execute(httpclient, post, soTimeout, --rsn);
//            }
//        }
//        throw new Exception("远程调用execute失败！");
//    }
//
//    /**
//     * 请求头设置
//     *
//     * @param post
//     * @throws Exception
//     */
//
//    /**
//     * https
//     *
//     * @param httpclient
//     * @throws Exception
//     */
//    public static void setHttps(HttpClient httpclient) throws Exception {
//
//        SSLContext ctx = SSLContext.getInstance("TLS");
//
//        X509TrustManager tm = new X509TrustManager() {
//            @Override
//            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//            }
//
//            @Override
//            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//            }
//
//            @Override
//            public X509Certificate[] getAcceptedIssuers() {
//                return null;
//            }
//        };
//
//        ctx.init(null, new TrustManager[] { tm }, null);
//        // 不验证域名
//        SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
//
//        Scheme sch = new Scheme("https", 443, ssf);
//        httpclient.getConnectionManager().getSchemeRegistry().register(sch);
//    }
//
//}
